package com.lzymall.controller.backend;

import com.lzymall.common.Const;
import com.lzymall.common.ResponseCode;
import com.lzymall.common.ServiceResponse;
import com.lzymall.pojo.Category;
import com.lzymall.pojo.User;
import com.lzymall.service.ICategoryService;
import com.lzymall.service.IUserService;
import com.lzymall.util.CookieUtil;
import com.lzymall.util.JsonUtil;
import com.lzymall.util.RedisShardedPoolUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: lizhenyao
 * @Desciption:
 */
@Controller
@RequestMapping("/manage/category")
public class CategoryManagerController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ICategoryService iCategoryService;

    @RequestMapping(value = "add_category.do")
    @ResponseBody
    public ServiceResponse addCategory(HttpServletRequest request, String categoryName, @RequestParam(value = "parentId", defaultValue = "0") int parentId){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return  ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //确定为管理员,开始执行管理员的逻辑
//            return iCategoryService.addCategory(categoryName, parentId);
//        }
//        return ServiceResponse.createByErrorMessage("无权限操作,需要管理员权限");
        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iCategoryService.addCategory(categoryName, parentId);
    }

    @RequestMapping(value = "set_category_name.do")
    @ResponseBody
    public ServiceResponse setCategoryName(HttpServletRequest request, Integer categoryId, String categoryName){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return  ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //确定为管理员,开始更新CategoryName的逻辑
//            return iCategoryService.updateCategoryName(categoryId, categoryName);
//        }
//        return ServiceResponse.createByErrorMessage("无权限操作,需要管理员权限");

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iCategoryService.updateCategoryName(categoryId, categoryName);
    }

    @RequestMapping(value = "get_category.do")
    @ResponseBody
    public ServiceResponse<List<Category>> getChildrenParallelCategory(HttpServletRequest request, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return  ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //查询自节点的category的信息,并且不递归
//            return iCategoryService.getChildrenParallelCategory(categoryId);
//        }
//        return ServiceResponse.createByErrorMessage("无权限操作,需要管理员权限");

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iCategoryService.getChildrenParallelCategory(categoryId);
    }

    @RequestMapping(value = "get_deep_category.do")
    @ResponseBody
    public ServiceResponse<List<Integer>> getCategoryAndDeepChildrenCategory(HttpServletRequest request, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return  ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            //查询自节点的category的信息,并且递归
//            return iCategoryService.getCategoryAndDeepChildrenCategory(categoryId);
//        }
//        return ServiceResponse.createByErrorMessage("无权限操作,需要管理员权限");

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iCategoryService.getCategoryAndDeepChildrenCategory(categoryId);
    }




}
